from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config.setting import MySQLConfig
from flask_cors import CORS
from flask_mail import Mail
from config import email_configure
from casbin import Enforcer
from casbin_sqlalchemy_adapter import Adapter


# def init_casbin():
#     adapter = Adapter("mysql+pymysql://root:123456@localhost:3306/e-web", True)
#     enforcer = Enforcer(adapter)
#     return enforcer


db = SQLAlchemy()
cors = CORS()
mail = Mail()


def create_app(__name__):
    app = Flask(__name__)
    app.config.from_object(MySQLConfig)
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
    app.config.from_object(email_configure)
    # app.enforcer = init_casbin()
    mail.init_app(app)
    db.init_app(app)
    cors.init_app(app)
    from routes.views.index import index_service
    from routes.views.register import register_service
    from routes.views.login import login_service
    from routes.views.goods_type import goods_type_service
    from routes.views.goods import goods_service
    from routes.views.user import userinfo
    from routes.views.gitee_login import gitee_login_service
    from routes.views.shopping import shopping
    from routes.views.order import order_service
    from routes.views.show_shop import show_shop
    from routes.views.analytics import analytics_service
    from routes.views.chat import chat_service
    from routes.views.comment import comment_service
    app.register_blueprint(index_service)
    app.register_blueprint(register_service)
    app.register_blueprint(login_service)
    app.register_blueprint(goods_type_service)
    app.register_blueprint(goods_service)
    app.register_blueprint(userinfo)
    app.register_blueprint(gitee_login_service)
    app.register_blueprint(shopping)
    app.register_blueprint(order_service)
    app.register_blueprint(show_shop)
    app.register_blueprint(analytics_service)
    app.register_blueprint(chat_service)
    app.register_blueprint(comment_service)
    # userinfo.before_request(casbin_middleware(app.enforcer))
    return app
